% Is there a common directory for both phase and color images?
%preprefix = '/Users/slampoud/Desktop/Mike_sample/';
%experiment='MF220';

subexperiment='xy2';

%preprefix = 'C:\Users\Michael Ferry\experiments\MF220\expt1001\';
%preprefix = ['C:\Users\Michael Ferry\experiments\' experiment '\expt1001\']

preprefix = ['C:\Users\Michael Ferry\Desktop\Bridget\ymc003\'];
%output_write_seg = 'C:\Users\Michael Ferry\experiments\MF220\analysis_data\xy2\segmented\';
%output_write_seg = ['C:\Users\Michael Ferry\experiments\' experiment '\analysis_data\' subexperiment '\segmented\']
output_write_seg = ['C:\Users\Michael Ferry\Desktop\Bridget\segmented\'];
%output_write_mask = 'C:\Users\Michael Ferry\experiments\MF220\analysis_data\mask\';
%output_write_color='C:\Users\Michael Ferry\experiments\MF220\analysis_data\color\';
%output_write_color_long='C:\Users\Michael Ferry\experiments\MF220\analysis_data\color_long\';
%output_write_traj='C:\Users\Michael Ferry\experiments\MF220\analysis_data\traj\';

% Where are the phase contrast image files?
%phaseprefix = [preprefix 'c1/'];
phaseprefix= preprefix;

% What's the pattern for picking out the phase images?
%phasepattern = '*xy2c1.tif';

phasepattern = ['*' subexperiment 'c1.tif']

% Where are the files for the color images?
% colorprefixes = [preprefix 'c2/';
%     preprefix 'c3/';
%     preprefix 'c4/'];

colorprefixes = [preprefix;
    preprefix;
    preprefix];

% % What are the patterns for picking out the color images?
% colorpatterns = [['*' subexperiment 'c2.tif'];
%     ['*' subexperiment 'c3.tif'];
%     ['*' subexperiment 'c4.tif']];
% colorpatterns
% % number of colors?
% numcolors=3;

% What are the patterns for picking out the color images?
colorpatterns = [['*' subexperiment 'c2.tif'];
    ['*' subexperiment 'c3.tif']];

colorpatterns
% number of colors?
numcolors=2;

% If the phase and color images are on the same timepoints, set step=1. 
% If the phase images are more frequent, set step to the interval, e.g. step=5.
step = 3;

% % What is the lower threshold for mask creation from the phase contrast images?
% threshold = 9500;
% 
% % What is the minimum length of an interesting trajectory?
% minlength = 100;
% 
% % What file do you want the trajectories to be saved to?
% trajfilename = 'trajectories-longtrajectories.mat';

%%%% PROCESS
% prepare paths

phasefilelist = dir([phaseprefix phasepattern]);
phasefilenames = {phasefilelist.name}';

colorfilenames = {};

for cn = 1:numcolors
   cf = dir([colorprefixes(cn,:) colorpatterns(cn,:)]);
   colorfilenames(cn,:) = {cf.name};
end

nfiles = size(phasefilenames,1);
%nfiles = 20;


% threshold & segment the images

% parfor fi=1:nfiles
%     ['segmentation loop: ' num2str(fi) ' ' datestr(now)]
%     phase = imread([phaseprefix phasefilenames{fi}]);
%     %mask  = ApplyLowerThreshold(phase, threshold);
%     mask = ApplyUpperThreshold(phase, threshold);
%     %segmented = im2bw(phase, threshold);
%     %segmented = ~mask;
%     %segmented = watershed(mask);
%     %mask  = edge(phase, 'nothinning');
%     %segmented = SegmentMask(CleanMask(CloseMask(mask), 10),2);
%     
%     %segmented = SegmentMask(mask,2);
%     
%     %segmented = CleanMask(CloseMask(mask), 10);
%     %segmented = mask;
%     imwrite(mask, [output_write_mask 'mask-' phasefilenames{fi}], ...
% 	    'tif', 'Compression', 'none');
% %     imwrite(segmented, [output_write_seg 'segmented-' phasefilenames{fi}], ...
% % 	    'tif', 'Compression', 'none');
% end

['segmentation finished']

%segmented mask filenames
%segfilelist = dir([output_write_seg 'segmented-*']);
%segfilelist = dir([output_write_seg 'segment*']);
segfilelist = dir([output_write_seg 'Mask*']);
segfilenames = {segfilelist.name}';

% [ObjectsByFrame] =  ObtainObjects(step,output_write_seg, ...
%      segfilenames, colorprefixes, colorfilenames, numcolors);

%[ObjectsByFrame,BinnedObjects] =  ObtainObjectsBinned(step,output_write_seg, ...
     %segfilenames, colorprefixes, colorfilenames, numcolors);
 [ObjectsByFrame,imageDimensions] =  ObtainObjects(step,output_write_seg,segfilenames, colorprefixes, colorfilenames, numcolors);
 [BinnedObjects] =  BinObjects(ObjectsByFrame,imageDimensions);

 ['objects obtained']
% % track trajectories
% [trajectories] = GetTrajectoriesOnlineStep(step,  ...
%  output_write_seg, output_write_color, segfilenames, colorprefixes, colorfilenames, numcolors);
% 
% ['trajectory building finished']
% 
% pick out long trajectories
% longtrajectories = SelectTrajectories(trajectories, minlength);
% 
% % save trajectory information
% save([output_write_traj trajfilename], 'trajectories', 'longtrajectories', '-MAT');
% 
% ['trajectories saved, now coloring']
% 
% % color long trajectories
% ColorLongTrajectoriesOnline(trajectories, longtrajectories, output_write_seg...
% ,output_write_color_long, segfilenames);
